package com.jianyi.system.shiro;

import com.jianyi.common.Constants;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.DefaultHashService;
import org.apache.shiro.crypto.hash.HashRequest;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.apache.shiro.util.SimpleByteSource;

public class ShiroEncrypt {

	/*public static String encrypt(String password){
		DefaultHashService hashService = new DefaultHashService(); //默认算法SHA-512
		hashService.setHashAlgorithmName("MD5");
		hashService.setPrivateSalt(new SimpleByteSource("123")); //私盐，默认无
		//hashService.setGeneratePublicSalt(true);//是否生成公盐，默认false
		//hashService.setRandomNumberGenerator(new SecureRandomNumberGenerator());//用于生成公盐。默认就这个
		hashService.setHashIterations(1); //生成Hash值的迭代次数

		HashRequest request = new HashRequest.Builder()
				.setAlgorithmName("MD5").setSource(ByteSource.Util.bytes(password))
				.setSalt(ByteSource.Util.bytes("123")).setIterations(1).build();
		String hex = hashService.computeHash(request).toHex();
		return hex;
	}*/

    public static String encrypt(String userName,String password){
        SimpleHash hex = new SimpleHash(
                Constants.ALGORITHMNAME,
                password,
                ByteSource.Util.bytes(userName+ Constants.CREDENTIALS_SALT),
                Constants.HASHITERATIONS);
        return hex.toHex();
    }

	public static void main(String[] args) {
        String md5 = encrypt("test", "222222");
		System.out.println(md5);
	}
}
